
%macro prnt2(program=, disname=, ds_list=, vis=, popfl=);

%get_data(lst=adex adsl,pre=,lib=adamw,supp=y);

proc format;
value $trtfor
"Very Low Nicotine Cigarettes"="A"
"Usual Brand Cigarettes"="B"
"Nicorette Gum"="C"
;
run;

data adex1(drop=studyid armcd enrlfl complfl pkfl pdfl trtsdt trtsdtm trtedt trtedtm trtpn trtp avisit asttm astdy aendt aentm aendy srcdom randafl randbfl exgrpid);
length dursec 8.;
set adex ;
by usubjid;
where avisitn in (4 5 6) and saffl="Y";
/*where avisitn in &vis and &popfl="Y";*/
trt=put(trta,trtfor.);
arm = "Study Product "||strip(trt);
column = trtan/10;
tocount=1;
/*if ASTDTM ne . and AENDTM ne . then dursec=AENDTM-ASTDTM+1;*/
if ASTDTM ne . and AENDTM ne . then dursec=AENDTM-ASTDTM;
/*else dursec=0;*/
trtan=trtan/10;
run;

data adex1;
length _st st $20.;
merge adex1(in=aaa) adsl (keep=usubjid trt04a trt05a trt06a);
by usubjid;
if aaa;
_st=strip(put(ASTDT,??is8601da.));
/*if index(trta,"Gum")=0 and index(parcat1,"Uncontrolled") then st=strip(put(ASTDTM,??is8601dt.)); */
/*else if index(trta,"Gum")=0 and index(parcat1,"Controlled") then st=strip(put(ASTDTM,??is8601dt.)); */
if index(trta,"Gum")=0 then st=strip(put(ASTDTM,??is8601dt.)); 
else st=strip(_st)||"T00:00:00"; 
run;

proc sort data=adex1 out=adex1; by usubjid avisitn astdt st trt trta parcat1 ASTDTM parcat2 srcseq; run;

*Keep Second use;

data adex2;
set adex1;
by usubjid avisitn astdt st trt trta parcat1 ASTDTM parcat2 srcseq;
if last.trta;
if index(aform,"GUM") and dosea ne . then _dosea=1;
else _dosea=dosea;
if parcat1=:"Uncontrol" then output;
run;

proc sort data=adex1; by trt; run;

proc sort data=adex2 out=pop nodupkeys;
  by trt arm column;
run;

Proc sort data =pop out = spopone nodupkey ;
  by trt arm column;
run;

data tofmt;
   set spopone;
run;

proc sort data=adex2 out=temporary;
by trt ;
run;

*Issue in Start of Inhalation and End of Inhalation addressed to DM On Aug-24. Two subjects have records of inhalation where duration lasted about one hour;
*Statement below check if issue resolved or not;

proc sort data=adex1 out=inh;
by subjid trt parcat2n;
where parcat2=:"Inh" and doseu=:"INHAL";
/*or parcat2=:"Sess";*/
/*where parcat2=:"Sess";*/
run;

/*data inh;*/
/*set inh;*/
/*by trt parcat2n;*/
/*if not first.trt eq last.trt and parcat2=:"Sess" then delete;*/
/*run;*/

*Count total duration of inhalations and subjects number in each trt;

proc sql;
create table totinh as select distinct subjid, sum(dursec) as totinh, trt from inh group by subjid, trt;
/*create table subj as select count(distinct subjid) as n, trtan, 1 as level2, 4 as level1  from inh group by trtan;*/
create table subj as select count(distinct subjid) as n, trtan, 1 as level2, 4 as level1  from temporary  where parcat2="Session" group by trtan;
quit;

proc sort data=subj; by level1 level2 n; run;
proc transpose data=subj out=subjn(rename=(_name_=LEVEL2_LABEL)) prefix=col;
by level1 level2;
var n;
id trtan;
run;

*Convert numeric value of COLX to character;

%macro ntc(vr); 
_&vr=strip(put(&vr,best.)); drop &vr;
%mend;

data subjn3; length LEVEL2_LABEL $200.; set subjn; level1=4; %ntc(col1); %ntc(col2); run;
data subjn4; length LEVEL2_LABEL $200.; set subjn; level1=3; %ntc(col1); %ntc(col2); run;

data temporary1;
merge inh totinh;
by subjid trt ;
run;

proc sort data=temporary1; by trt; run;

*Gum Total duration;

proc sort data=temporary out=gum;
by subjid trt ;
where aform="GUM";
run;

proc sql;
create table totgum as select distinct subjid, sum(dursec) as totgum, trt from gum group by subjid, trt;
create table subjgum as select count(distinct subjid) as n, trtan, 1 as level2, 5 as level1  from gum group by trtan;
quit;

proc sort data=subjgum; by level1 level2 n; run;
proc transpose data=subjgum out=subjgum1(rename=(_name_=LEVEL2_LABEL)) prefix=col;
by level1 level2;
var n;
id trtan;
run;

data subjgum1; length LEVEL2_LABEL $200.; set subjgum1; %ntc(col3);  run;

data temporary2;
merge gum totgum;
by subjid trt ;
if index(aform,"GUM") and dosea ne . then _dosea=1;
else _dosea=dosea;
totgumm=totgum/60;
run;

proc sort data=temporary2; by trt; run;

*******************************************************************************************************
Define specific formats for the report
******************************************************************************************************;
proc format;
 value level1l  1 = "Total Number of Inhalations per Subject"
                2 = "Duration of Inhalations (sec)"
				3 = "Average Duration of Inhalations per Subject (sec)"
				4 = "Total Duration of Inhalations per Subject (sec)"
				5 = "Duration of Gum Product Use (min)"
;
run;

*******************************************************************************************************
Data manipulation
******************************************************************************************************;

%univariate6p(var=_dosea, width=5,decimal=0,out=_tmp1,level1=1,level1FMT=level1l,level2=,level2FMT=stat3_,level3=,wh=%str(index(parcat2,"Session") and _dosea ne .),
			by=, in=temporary);

proc sort data=temporary out=temporary; by column; run;
data temporary1; set temporary1; run;
data adex1; set adex1; run;
proc sort data=inh out=inh; by column; run;

%univariate6p(var=dursec, width=5,decimal=0,out=_tmp2,level1=2,level1FMT=level1l,level2=,level2FMT=stat3_,level3=,wh=%str(doseu="INHALATION" and parcat2n=10 ),
			by=, in=inh);

/*%univariate6p(var=dursec, width=5,decimal=0,out=_tmp2,level1=2,level1FMT=level1l,level2=,level2FMT=stat3_,level3=,wh=%str(doseu="PUFF" ),*/
/*			by=, in=adex1);*/

%univariate6p(var=dursec, width=5,decimal=0,out=_tmp3,level1=3,level1FMT=level1l,level2=,level2FMT=stat3_,level3=,wh=%str(parcat2="Inhalation" and parcat2 ne "Session" and dursec ne . ),
			by=, in=temporary);

/*%univariate6p(var=dursec, width=5,decimal=0,out=_tmp3,level1=3,level1FMT=level1l,level2=,level2FMT=stat3_,level3=,wh=%str(dursec ne . ),*/
/*			by=, in=temporary1);*/

%univariate6p(var=totinh, width=5,decimal=0,out=_tmp4,level1=4,level1FMT=level1l,level2=,level2FMT=stat3_,level3=,wh=%str(parcat2="Inhalation" and totinh ne . ),
			by=, in=temporary1);

/*%univariate6p(var=totinh, width=5,decimal=0,out=_tmp4,level1=4,level1FMT=level1l,level2=,level2FMT=stat3_,level3=,wh=%str(totinh ne . ),*/
/*			by=, in=temporary1);*/

%univariate6p(var=totgumm, width=5,decimal=0,out=_tmp5,level1=5,level1FMT=level1l,level2=,level2FMT=stat3_,level3=,wh=%str(aform="GUM" ),
			by=, in=temporary2);

*Number subject for header;

proc sql;
select count(distinct subjid) into :nba from temporary where trt="A";
select count(distinct subjid) into :nbb from temporary where trt="B";
select count(distinct subjid) into :nbc from temporary where trt="C";
quit;

%let nba=&nba; %let nbb=&nbb; %let nbc=&nbc;
%put &nba; %put &nbb; %put &nbc;

*******************************************************************************************************
Create report dataset
******************************************************************************************************;
data final;
set _tmp1 _tmp2 _tmp3 _tmp4 _tmp5; 
by level1 level2;
page=1;
run;

proc sort data=final out=final;
by level1 level2;
run;

*Align N;
data subjn1; set subjn3; level1=1; leve2=1; run; proc sort data=subjn1; by level1 level2; run; 
proc sort data=subjn3; by level1 level2; run; proc sort data=subjn4; by level1 level2; run; proc sort data=subjgum; by level1 level2; run;  

/*proc sql;*/
/*create table tinh as select distinct sum(dursec)/60 as totinh, trt, 1 as level2, 2 as level1 from adex1 where parcat2="Session" group by  trt;*/
/*quit;*/

proc sort data=subj out=subj; by level1 level2 n; run;
proc transpose data=subj out=subjn(rename=(_name_=LEVEL2_LABEL)) prefix=col;
by level1 level2;
var n;
id trtan;
run;

data inh; set inh; w=1; run;

proc sql;
create table thn as select distinct sum(w) as thn, 2 as level1, 1 as level2, trtan from inh group by trtan;
quit;
proc sort data=thn; by level1 level2 thn; run;
proc transpose data=thn out=thnn(drop=_name_) prefix=_c;
by level1 level2;
var thn;
id trtan;
run;

data report;
merge final thnn subjn1 subjn3 subjn4 subjgum1;
by level1 level2;
if _col1 ne " " then col1="  "||strip(_col1); else col1=col1;
if _col2 ne " " then col2="  "||strip(_col2); else col2=col2;
if _col3 ne " " then col3="  "||strip(_col3); else col3=col3;
if _c1 ne . then col1="  "||strip(put(_c1,best.)); else col1=col1;
if _c2 ne . then col2="  "||strip(put(_c2,best.)); else col2=col2;

if level1_label ne " " then do;
level2_label=lowcase(level2_label);
end;
drop _col1 _col2 _col3 _c1 _c2;
run;

proc sort data=report; by page level1 level2; run;

proc sql;
 select count(distinct level2_label)
 into :nb_obse
 from report;
quit;
%put &nb_obse;

*******************************************************************************************************
Printing using proc report
******************************************************************************************************;
title3 "Table &disname (Continued)";
%footer1(foot1=1, ds_list=&ds_list.,program=&program.);

filename filetmp temp;
filename filertf "\\algopharm.com\algorithmepharmadata\Biostudies\Montreal\&prot.\SRA\Biostatistics\Work\Outputs\tables\T&disname..PROD.USE.rtf";

%open_rtf;
%empty_ds_fix(report);

title4 "Summary of Second Product Use Part B";
title5 "(Safety Population) ";

/*footnote3 j=l "Note(s): N is by parameter either per subjects or per units.";*/
/*footnote4 j=l "         Estimated nicotine uptake = total cartridge weight change (mg) x nicotine concentration of the liquid (% by weight)";*/
/*footnote5 j=l " ";*/
/*footnote6 j=l " ";*/
/*footnote7 j=l " ";*/


proc report data=report nowd split='~' missing;
 column page level1 level2 level1_label  level2_label col1 col2 col3;
 define page         / " " order order=internal noprint;
 define level1       / " " order order=internal noprint;
 define level2       / "" order order=internal noprint;
 define level1_label / " " order order=internal style(column)=[cellwidth=3.5in just=left] style(header)=[just=left];
 define level2_label / " " order order=internal style(column)=[cellwidth=1.2in just=left] style(header)=[just=left];

 define col1 / " Study~Product A~(N=&nba)" style(column)=[cellwidth=1.6in just=c] style(header)=[just=c];
 define col2 / " Study~Product B~(N=&nbb)" style(column)=[cellwidth=1.6in just=c] style(header)=[just=c];
 define col3 / " Study~Product C~(N=&nbc)" style(column)=[cellwidth=1.6in just=c] style(header)=[just=c];

 break after page   / page;
 %if &nb_obse GT 0 %then %do;

 compute before level1 / style=[cellwidth=6in just=left];
  line " ";
 endcomp;
 %end;

 %if &nb_obse EQ 0 %then %do;
   compute after page/style=[cellwidth=9.7in just=left];
    line "No Values Were Measured.";
   endcomp;
 %end;

run;

ods rtf close;
ods listing;
%arrange_rtf;
%mend prnt2;

/*%prnt(program=T14_01_04_02_PROD_USE, disname=14.1.4.2, ds_list=ADEX, vis=%str((4 5 6), popfl=%str(SAFFL="Y"));*/
%prnt2(program=T14_01_04_02_PROD_USE, disname=14.1.4.2, ds_list=ADEX);

%clrw;
